//
//  TestViewController.m
//  Cycling
//
//  Created by chijianguo on 2023/3/20.
//

#import "TestViewController.h"

@interface TestViewController ()
@property (weak, nonatomic) IBOutlet UILabel *valueLB;
@property (weak, nonatomic) IBOutlet UITextField *cmdTF;
@property (weak, nonatomic) IBOutlet UILabel *fcLB;

@end

@implementation TestViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];

    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"蓝牙测试";
    // Do any additional setup after loading the view from its nib.
    [CycBLEManager sharedManager].receiveDatafcBlock = ^(NSData *data) {
        self.fcLB.text = [self hexStringFromData:data];
    };
}

- (IBAction)send:(id)sender {
    uint8_t cmd = (uint8_t)[self.cmdTF.text intValue];
    [self.car getCarMessageWithCmd:cmd retBlock:^(NSData * _Nonnull data, NSError * _Nonnull error) {
        if (data) {
            self.valueLB.text = [self hexStringFromData:data];
        }else {
            self.valueLB.text = @"消息发送失败";
        }
    }];
}

//data转换为十六进制的string
-(NSString *)hexStringFromData:(NSData *)myD{
    
    Byte *bytes = (Byte *)[myD bytes];
    //下面是Byte 转换为16进制。
    NSString *hexStr=@"";
    for(int i=0;i<[myD length];i++)
    {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
        if([newHexStr length]==1)
            hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
        else
            hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    }
    NSLog(@"hex = %@",hexStr);
    return hexStr;
}

@end
